package com.yicong.ants.ui.me;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.google.gson.Gson;
import com.mbridge.msdk.playercommon.exoplayer2.util.ColorParser;
import com.yicong.ants.R;
import com.yicong.ants.bean.CheckDestroyAccountBean;
import com.yicong.ants.databinding.ActivityCancellationStatusConfirmationBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.utils.ConstantCodeUtils;

import java.util.List;

/**
 * 注销账号状态确认
 */
public class CancellationStatusConfirmationActivity extends BaseTitleBarActivity<ActivityCancellationStatusConfirmationBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.activity_cancellation_status_confirmation;
    }

    @Override
    protected void initEventAndData() {
        AppHelper.transStatus(getWindow());
        setTitleText("注销账号");
        setTitleBarVisible(false);

        String str = getIntent().getStringExtra("data");
        CheckDestroyAccountBean data = new Gson().fromJson(str, CheckDestroyAccountBean.class);
        data.getValidRose();
        if (data.getValidRose().size() > 0) {
            mDataBind.tvTips.setText("抱歉，您暂时无法注销账号！");
            mDataBind.tvTips2.setText("请确认以下身份的账户已停用，才可注销");

            setTipsGroup(data.getValidRose(), true);

            mDataBind.next.setVisibility(View.GONE);
            mDataBind.back2.setText("确认");
        } else if (data.getTradeRefundIng().size() > 0) {
            mDataBind.tvTips.setText("抱歉，您还有处于退款中的订单！");
            mDataBind.tvTips2.setText("系统检测到有正在处理退款的订单，请处理完成后，再注销账户");

            setTipsGroup(data.getTradeRefundIng(), false);
        }

        mDataBind.setClick(this);
    }

    private void setTipsGroup(List<String> list, boolean show) {
        mDataBind.tipsGroup.removeAllViews();
        if (show) {
            list.add("说明：可先联系平台客服停用对应角色的账户，然后重新进行账户注销");
        }
        for (int i = 0; i < list.size(); i++) {
            TextView textView = new TextView(this);
            textView.setTextSize(14);
            if (show && i == list.size() - 1) {
                textView.setTextColor(ColorParser.parseCssColor("#FF4343"));
                textView.setText(list.get(i));
            } else {
                textView.setTextColor(ContextCompat.getColor(this, R.color.new_text_color_2));
                textView.setText((i + 1) + "." + list.get(i));
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i == 0) {
                params.setMargins(0, 0, 0, 0);
            } else {
                params.setMargins(0, SizeUtils.dp2px(6), 0, 0);
            }

            mDataBind.tipsGroup.addView(textView, params);
        }
    }

    @Override
    protected void onLoadData() {
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back || id == R.id.back2) {
            setResult(ConstantCodeUtils.LOGGED_OUT);
            onBackPressed();
        } else if (id == R.id.next) {
            Router.turnTo(mContext, CancellationNextActivity.class).startForResult(102);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case ConstantCodeUtils.LOGGED_OUT:
                setResult(ConstantCodeUtils.LOGGED_OUT);
                finish();
                break;
            default:
                break;
        }
    }
}
